package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Choice;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.ChoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "选择题接口",value = "choice")
@RestController
@RequestMapping("/choice")
public class ChoiceController {
    @Autowired
    private ChoiceService choiceService;

    @ApiOperation(value = "增加选择题")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Choice choice){
        //对于插入的选择题判断是否重复
        try{
            if (choiceService.selectByContent(choice.getContent()) != null){
                return R.error("这道题目已经在题库当中存在了");
            }
            choiceService.insert(choice);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "选择题目")
    @GetMapping("/findByChoiceId/{id}")
    public R findByChoiceId(@PathVariable Integer id){
        try{
            return R.ok().put("data",choiceService.selectById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "删除题目")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        try{
            choiceService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "修改题目")
    @PostMapping("/update")
    public R update(Choice choice){
        try {
            choiceService.update(choice);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
